package com.margo.rabbit.springboot.consumer;

import java.io.IOException;
import java.util.Map;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;

/**
 * https://blog.csdn.net/Sadlay/article/details/86716028
 * 		消息序列化
 * 
 * https://blog.csdn.net/alex_xfboy/article/details/83342164
 * 	ObjectProvider
 * @author fangxingbang
 *
 */
@Component
public class MargoDirectConsumer {

	@RabbitListener(queues="margo-direct")
	@RabbitHandler // 指定对消息的处理
	public void directExchange(Channel channel,@Payload String body,@Headers Map<String, Object> headers) {
		/**
		 * margo-direct receive msg : 109,97,114,103,111
		 * 
		 * AmqpHeaders 中定义
		 * amqp_receivedRoutingKey:margo-direct
		 * amqp_deliveryTag:100
		 * amqp_consumerQueue:margo-direct
		 * amqp_redelivered:false
		 * id:fea815b3-6fac-be4e-b13e-bd0a003b8c26
		 * amqp_consumerTag:amq.ctag-s7b6c6q9jaH2v3x0dDVJpw
		 * timestamp:1575441912699
		 */
		System.out.println("margo-direct receive msg : " +  body);
		for(String key:headers.keySet()) {
			System.out.println(key+":"+headers.get(key));
		}
		try {
			channel.basicAck((Long)headers.get("amqp_deliveryTag"),false);
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}
}
